package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuSaveVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;

    @Override
    public void saveSpuInfo(SpuSaveVo spuSaveVo) {
        //1、保存到spu_info表中
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spuSaveVo,spuInfo);
        save(spuInfo);
        Long spuId = spuInfo.getId();

        //2、保存图片到spu_image表中
        //。。dto 需要转换成SpuImage，并设置spuId属性

        /*List<SpuSaveVo.SpuImageListDTO> spuImageDtoList = spuSaveVo.getSpuImageList();
        List<SpuImage> spuImageList = new ArrayList<>();
        for (SpuSaveVo.SpuImageListDTO dto : spuImageDtoList) {
            SpuImage spuImage = new SpuImage();
            BeanUtils.copyProperties(dto, spuImage);
            spuImage.setSpuId(spuId);
            spuImageList.add(spuImage);
            spuImageService.saveBatch(spuImageList);
        }*/

        //streamApi
        List<SpuImage> spuImageList = spuSaveVo.getSpuImageList()
                .stream()
                .map(item -> {
                    SpuImage spuImage = new SpuImage();
                    BeanUtils.copyProperties(item, spuImage);
                    spuImage.setSpuId(spuId);
                    return spuImage;
                })
                .collect(Collectors.toList());
        spuImageService.saveBatch(spuImageList);

        //3、保存销售属性名到spu_sale_attr
        List<SpuSaleAttr> spuSaleAttrList = spuSaveVo.getSpuSaleAttrList()
                .stream()
                .map(item -> {
                    SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
                    BeanUtils.copyProperties(item,spuSaleAttr);
                    spuSaleAttr.setSpuId(spuId);
                    return spuSaleAttr;
                })
                .collect(Collectors.toList());
        spuSaleAttrService.saveBatch(spuSaleAttrList);


        //4、保存销售属性值到spu_sale_attr_value
        List<SpuSaleAttrValue> attrValues = spuSaveVo.getSpuSaleAttrList().stream()
                .flatMap(item -> { //打散、拉平
                    return item.getSpuSaleAttrValueList().stream()
                            .map(valueDto -> {
                                SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();
                                spuSaleAttrValue.setSpuId(spuId);
                                spuSaleAttrValue.setBaseSaleAttrId(valueDto.getBaseSaleAttrId());
                                spuSaleAttrValue.setSaleAttrValueName(valueDto.getSaleAttrValueName());
                                spuSaleAttrValue.setSaleAttrName(item.getSaleAttrName());
                                return spuSaleAttrValue;
                            });

                }).collect(Collectors.toList());
        spuSaleAttrValueService.saveBatch(attrValues);
    }
}




